package dth.bean;
import java.sql.Date;

import javax.faces.bean.ManagedBean;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.dao.FaqDAO;
import dth.dao.PackageDAO;
import dth.entity.Faq;
import dth.entity.Package;
import dth.utils.DthUtil;
import antlr.collections.List;

@ManagedBean(name="faqBean")
public class FaqBean {
	private int id=0;
	private String title;
	private String content;
	private int fkAdminID=1;
	public String getTitle() {
		return title;
	}

	public int getFkAdminId() {
		return fkAdminID;
	}

	public void setFkAdminId(int fkAdminId) {
		fkAdminID = fkAdminId;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public java.util.List<Faq> getListFaq()
	{
		FaqDAO  faqDAO=new FaqDAO();
		return faqDAO.getListFaq();
	}
	public FaqBean() {
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("faq.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		// TODO Auto-generated constructor stub
		System.out.println("--faq bean");
		Object objectAction=DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:"+objectAction);
		if(objectAction!=null)
		{
			if(objectAction.equals("delete"))
			{
				Object objectId=DthUtil.getRequest().getParameter("faq_id");
				if(objectId!=null)
					id=Integer.parseInt(objectId.toString());
				System.out.println("---id delete:"+id);
			}
			else if(objectAction.equals("update"))
			{
				Object objectId=DthUtil.getRequest().getParameter("faq_id");
				if(objectId!=null)
					id=Integer.parseInt(objectId.toString());
				System.out.println("---id edit:"+id);
				for(Faq faq:getListFaq())
				{
					if(faq.getFaqId()==id)
					{
						this.title=faq.getFaqTitle();
						this.content=faq.getFaqContent();
					}
				}
				System.out.println("-----name:"+title);
			}
		}
	}
	
	public void insert()
	{
		System.out.println("---id="+id);
		try{
			FaqDAO faqDAO=new FaqDAO();
			Faq faq1=new Faq();
			faq1.setFaqId(id);
			faq1.setFaqContent(content);
			faq1.setFkAdminId(1);
			faq1.setFaqTitle(title);
			
			java.util.Date date=new java.util.Date();//lay ngay hien tai
			faq1.setDateInsert(new Date(date.getTime()));
			int count=0;
			if(id==0)
				count=faqDAO.insert(faq1);
			else
				count=faqDAO.update(faq1);
			if(count>0)
				DthUtil.showMessage("ThĂ´ng bĂ¡o", "Ä�Ă£ insert thĂ nh cĂ´ng");
			else
				DthUtil.showMessage("ThĂ´ng bĂ¡o", "Ä�Ă£ insert tháº¥t báº¡i");
		}
		catch(Exception ex){
			DthUtil.showMessage("ThĂ´ng bĂ¡o", "Ä�Ă£ insert tháº¥t báº¡i");
		}
	}
	
	public void delete()
	{
		System.out.println("---delete faq id:"+id);
		FaqDAO  faqDAO=new FaqDAO();
		int count=faqDAO.delete(id);
		if(count>0)
			DthUtil.showMessage("ThĂ´ng bĂ¡o", "Ä�Ă£ xĂ³a thĂ nh cĂ´ng");
		else
			DthUtil.showMessage("ThĂ´ng bĂ¡o", "Ä�Ă£ xĂ³a tháº¥t báº¡i");
	}
}
